package cn.jg.web.controller.sys.rest;

import cn.jg.framework.web.controller.BaseController;
import cn.jg.framework.web.controller.response.BaseResponse;
import cn.jg.model.sys.Company;
import cn.jg.service.sys.CompanyService;
import cn.jg.web.controller.sys.request.CompanyCreateRequest;
import cn.jg.web.controller.sys.request.CompanyUpdateRequest;
import cn.jg.web.controller.sys.response.CompanyFoundResponse;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Optional;

/**
 * Created by admin on 2023/06/06.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/companys")
public class CompanyController extends BaseController{

    private final CompanyService companyService;

    /**
     * 获取详情
     *
     * @param id ID
     * @return {@link CompanyFoundResponse}
     */
    @RequiresAuthentication
    @GetMapping(value = "/{id:^[1-9]\\d*$}", produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<CompanyFoundResponse>> findById(@PathVariable(value = "id") final int id) {
        Optional<Company> possible = companyService.findById(id);
        if (!possible.isPresent()) {
            return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NOT_FOUND);
        }

        CompanyFoundResponse response = new CompanyFoundResponse().convert(possible.get());
        return new ResponseEntity<>(new BaseResponse<>(response), foundHttpStatus(response));
    }

    /**
     * 获取列表
     *
     * @param pageNum 页数
     * @param pageSize 页数大小
     * @return {@link CompanyFoundResponse}
     */
    @RequiresAuthentication
    @GetMapping(produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<List<CompanyFoundResponse>>> findAll(
        @RequestParam(value = "pageNum", required = false, defaultValue = "1") @Min(1) final int pageNum,
        @RequestParam(value = "pageSize", required = false, defaultValue = "20") @Min(1) @Max(50) final int pageSize) {
        List<Company> result = companyService.findAll(pageNum, pageSize);
        List<CompanyFoundResponse> response = new CompanyFoundResponse().convertToList(result);
        return new ResponseEntity<>(new BaseResponse<>(response), foundHttpStatus(response));
    }


    /**
     * 保存
     *
     * @param companyCreateRequest {@link CompanyCreateRequest}
     * @return {@link CompanyFoundResponse}
     */
    @RequiresAuthentication
    @PostMapping(consumes = APPLICATION_JSON, produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<CompanyFoundResponse>> save(@Valid @RequestBody final CompanyCreateRequest companyCreateRequest) {
        Company result = companyService.save(companyCreateRequest.convert(companyCreateRequest));
        CompanyFoundResponse response = new CompanyFoundResponse().convert(result);
        return new ResponseEntity<>(new BaseResponse<>(response), HttpStatus.CREATED);
    }

    /**
     * 更新
     *
     * @param id ID
     * @param companyUpdateRequest {@link CompanyUpdateRequest}
     * @return Void
     */
    @RequiresAuthentication
    @PutMapping(value = "/{id:^[1-9]\\d*$}", consumes = APPLICATION_JSON, produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<Void>> update(@PathVariable(value = "id") final int id,
        @Valid @RequestBody CompanyUpdateRequest companyUpdateRequest) {
        if (!companyService.exists(id)) {
            return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NOT_FOUND);
        }
        Company company = companyUpdateRequest.convert(companyUpdateRequest);
        company.setId(id);
        companyService.update(company);
        return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NO_CONTENT);
    }

    /**
    * 删除
    *
    * @param id ID
    * @return Void
    */
    @RequiresAuthentication
    @DeleteMapping(value = "/{id:^[1-9]\\d*$}", produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<Void>> deleteById(@PathVariable(value = "id") final int id) {
        if (!companyService.exists(id)) {
        return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NOT_FOUND);
        }
        companyService.delete(id);
        return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NO_CONTENT);
    }
}
